﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Context;
using Eirene.Core.Model;

namespace Eirene.CLI
{
    /// <summary>
    /// Application to build up knowledge of IOC.
    /// </summary>
    public class Program
    {
        public const string ApplicationMainObjectId = "Main";

        /// <summary>
        /// Bootstrap a <see cref="Main"/> object for the application via spring
        /// </summary>
        /// <remarks>
        /// See http://www.springframework.net/docs/1.3.0/reference/html/index.html
        /// </remarks>
        static void Main(string[] args)
        {
            // Get Spring.NET's IoC container.
            IApplicationContext ioc;
            ioc = Spring.Context.Support.ContextRegistry.GetContext();

            // Bootstrap the main object.
            var main = ioc.GetObject(ApplicationMainObjectId) as Main;

            if (main == null)
            {
                var msg = String.Format(
                    "Application Main object is not a {0}"
                    + " Check IoC config defines the object {1} correctly."
                    , typeof(Main).FullName
                    , ApplicationMainObjectId);
                throw new ApplicationException(msg);
            }

            // Get the message service.
            main.WelcomeMessage.DisplayMessage();

            // Display the list of existing items.
            main.DisplayItemList("Existing");

            // Add a new item.
            main.AddNewItem();

            // Display the updated list.
            main.DisplayItemList("Updated");

            // Exit the application.
            Console.WriteLine("\n\nPress key to exit.");

            Console.ReadKey();
        }
    }
}
